package _10_binary_tree.invert;

import _10_binary_tree.TreeNode;
import org.junit.Test;

/**
 * @author: mornd
 * @dateTime: 2023/6/12 - 2:06
 * 翻转二叉树，如图
 * 从第二层开始，左孩子变右孩子
 *
 *
 *     1
 *    /   \
 *   2     3
 *  / \
 * 4   5
 *    / \
 *   7   8
 *
 *     👇
 *
 *     1
 *    /   \
 *   3     2
 *        / \
 *       5   4
 *      / \
 *     8   7
 */
public class MyTest {
    @Test
    public void test() {
        // chatgpt 构建
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.left.right.left = new TreeNode(7);
        root.left.right.right = new TreeNode(8);
        root.right = new TreeNode(3);
        System.out.println(root.printTreeStructure());
        invert(root);
        System.out.println(root.printTreeStructure());
    }

    // 翻转
    TreeNode invert(TreeNode root) {
        if(root == null) {
            return null;
        }
        TreeNode left = invert(root.left);
        // 左侧转为翻转后的右侧
        root.left = invert(root.right);
        root.right = left;
        return root;
    }
}
